const asyncHooks = require("async_hooks");

//注意观察 aid和tid
console.log(`aid ${asyncHooks.executionAsyncId()}, tid ${asyncHooks.triggerAsyncId()}`);

function funcA()
{
    console.log(`funcA: aid ${asyncHooks.executionAsyncId()}, tid ${asyncHooks.triggerAsyncId()}`);
}

funcA();


console.log("---------------------");

const fs = require("fs");

let indent = 0;
asyncHooks.createHook({
  init(asyncId, type, triggerAsyncId) {
    const eid = asyncHooks.executionAsyncId();
    const indentStr = ' '.repeat(indent);
    fs.writeSync(
      process.stdout.fd,
      `init ${indentStr}${type}(${asyncId}):` +
      ` trigger: ${triggerAsyncId} execution: ${eid}\n`);
  },
  before(asyncId) {
    const indentStr = ' '.repeat(indent);
    fs.writeSync(process.stdout.fd, `${indentStr}before:  ${asyncId}\n`);
    indent += 2;
  },
  after(asyncId) {
    indent -= 2;
    const indentStr = ' '.repeat(indent);
    fs.writeSync(process.stdout.fd, `${indentStr}after:  ${asyncId}\n`);
  },
  destroy(asyncId) {
    const indentStr = ' '.repeat(indent);
    fs.writeSync(process.stdout.fd, `${indentStr}destroy:  ${asyncId}\n`);
  },
}).enable();



//Promise.resolve(1).then(funcA);
//setTimeout(funcA, 100);

setTimeout(funcA, 1000);




